Skip to content

[GH-3178] Add Geography support for ST_KNN as a broadcast join - #3542

Merged
jiayuasu merged 2 commits into
apache:masterfrom
jiayuasu:feature/geography-knn-join
Nov 17, 2026
Merged

jiayuasu merged 2 commits into
apache:masterfrom
jiayuasu:feature/geography-knn-join

Conversation

@jiayuasu

@jiayuasu jiayuasu commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Did you read the Contributor Guide?

Is this PR related to a ticket?

What changes were proposed in this PR?

ST_KNN(geography, geography, k) joins each query row with its k nearest object rows. Neighbours are ranked by the distance that ST_Distance returns for two geographies: the minimum distance on the sphere, in meters. Only the 3-argument form accepts geographies. The 4-argument form stays geometry-only, and mixed geometry / geography arguments are rejected at analysis time.

This PR covers the broadcast join, on top of the index added in #3543. A partitioned geography KNN join is left for a follow-up.

  • ST_KNN gets a (Geography, Geography, Int) overload.
  • JoinQueryDetector plans geography KNN as a new BroadcastGeographyKNNJoinExec. Either side can be broadcast, by hint or by sedona.join.autoBroadcastJoinThreshold.
    • Object side broadcast: the objects are collected and indexed once, and every query row searches that index.
    • Query side broadcast: every partition of objects is indexed and searched by every query, and the per-partition neighbours of a query are merged into its k nearest.
  • Neighbours are found with GeographyKnnIndex from [GH-3178] Add a k-nearest-neighbour index for Geography #3543, a best-first search over the latitude / longitude bounding rectangles of the geographies that prunes with the distance on the sphere between rectangles. This keeps the result correct across the antimeridian and around the poles, where a planar bound in degrees is not a lower bound.
  • JoinedGeometry.geographyToEnvelopeGeometry, used by the other geography joins, now takes its envelope from GeographyKnnIndex.toEnvelope instead of repeating the same logic.
  • ST_KNN cannot be evaluated one pair at a time, so there is no row-by-row fallback. A geography KNN join with no broadcast side, or with a join type other than inner, fails at planning time with an error that says what to change.
  • NULL and empty geographies, spark.sedona.join.knn.includeTieBreakers, and residual ON predicates behave as they do for the geometry KNN join.

How was this patch tested?

New KnnJoinGeographySuite runs every result test in four layouts (object or query side broadcast, queries or objects first in the join):

  • random points and polygons over the whole globe, including polygons that cross the antimeridian and points near the poles, compared with a brute-force cross join ranked by ST_Distance
  • nearest neighbour across the antimeridian, across a pole, and at a latitude where planar degrees give a different order
  • minimum distance to a polygon rather than distance to its centroid
  • k larger than the object count, NULL and empty inputs, ties with and without tie breakers, residual conditions, the DataFrame API, empty sides
  • planning: both broadcast sides, auto broadcast, the errors for no broadcast side and for a non-inner join, analysis errors for the 4-argument form and for mixed arguments
  • the example in the documentation

New BroadcastGeographyKnnIndexSuite covers how rows travel between the driver and the executors: the broadcast index and the shuffled neighbour lists are used after Java and Kryo serialization. The search itself is tested in #3543.

Run locally together with the existing KnnJoinSuite, BroadcastIndexJoinGeographySuite, LegacyGeographyDistanceJoinSuite, BroadcastIndexJoinSuite, predicateJoinTestScala and predicateTestScala on Spark 3.5 / Scala 2.12, and with KnnJoinSuite and the geography suites on Spark 4.2 / Scala 2.13.

Did this PR include necessary documentation updates?

@jiayuasu
jiayuasu marked this pull request as draft November 12, 2026 08:48
@jiayuasu
jiayuasu force-pushed the feature/geography-knn-join branch from c621149 to 2efd59f Compare November 13, 2026 07:04
@jiayuasu
jiayuasu force-pushed the feature/geography-knn-join branch 4 times, most recently from fba85cf to 4f704de Compare November 17, 2026 07:43
ST_KNN(geography, geography, k) joins each query row with its k nearest
object rows, ranked by the distance that ST_Distance returns for two
geographies: the minimum distance on the sphere, in meters. Only the
3-argument form accepts geographies, because spherical ranking is the
definition for geography rather than an option.

The join is planned as BroadcastGeographyKNNJoinExec with either side
broadcast. Geographies are indexed by their latitude / longitude bounding
rectangle, and a best-first search prunes by the distance on the sphere
between rectangles, so neighbours are found correctly across the
antimeridian and around the poles.

ST_KNN cannot be evaluated one pair at a time, so a geography KNN join
that has no broadcast side, or is not an inner join, is rejected at
planning time with an error that says what to change. A partitioned
geography KNN join is left for a follow-up.
@jiayuasu
jiayuasu force-pushed the feature/geography-knn-join branch from 4f704de to 0e316f8 Compare November 17, 2026 08:56
@jiayuasu
jiayuasu marked this pull request as ready for review November 17, 2026 08:56
@jiayuasu jiayuasu added this to the sedona-2.0.0 milestone Nov 17, 2026
@jiayuasu
jiayuasu merged commit 337d4a4 into apache:master Nov 17, 2026
43 checks passed
@jiayuasu
jiayuasu deleted the feature/geography-knn-join branch November 20, 2026 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Geography support for ST_KNN

1 participant